package com.itmake.sample.toolbox.file;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Objects;

/**
 * 网络文件工具类
 *
 * @author Amjacks
 * @since 2023/08/10
 */
public class NetFileUtil {

    private NetFileUtil() {}

    /**
     * 获取网络文件
     *
     * @param path 路径
     * @return {@link BufferedInputStream}
     */
    public static BufferedInputStream getNetFile(String path){
        URL url = null;
        HttpURLConnection connection = null;
        BufferedInputStream imageIn = null;
        try {
            url = new URL(path);
            connection = (HttpURLConnection) url.openConnection();
            imageIn = new BufferedInputStream(connection.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (Objects.isNull(imageIn)) {
            throw new RuntimeException("图片不存在，地址为：" + path);
        }
        return imageIn;
    }
}
